package com.itheima.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.youdianda.common.CustomException;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.entity.*;
import com.itheima.youdianda.service.*;
import com.itheima.youdianda.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/index")
public class SystemController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysConfigService sysConfigService;


    @Autowired
    private YddAdvimgService yddAdvimgService;

    @Autowired
    private YddAdvposService yddAdvposService;

    @Autowired
    private YddCateService yddCateService;

    @Autowired
    private YddArticleService yddArticleService;

    @Autowired
    private YddUserService yddUserService;

    @Autowired
    private YddTagService yddTagService;


    @PostMapping("/login")
    public R login(HttpServletRequest request, @RequestBody SysUser sysUser){
        //1.将页面提交的密码password进行md5加密处理
        String password = sysUser.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getLogin_name,sysUser.getLogin_name());
        SysUser sUser = sysUserService.getOne(queryWrapper);

        //3.如果没有查询到返回登录失败
        if (sUser == null){
            throw new CustomException("用户名不存在");
            //return R.error("登录失败,没有该用户");
        }

        //4.密码比对,如果错误返回登录失败
        if (!sUser.getPassword().equals(password)){
            throw new CustomException("密码错误");
            //return R.error("登录失败,密码错误");
        }

        //5.查看员工状态,如果禁用,返回登陆失败
        if (sUser.getStatus() == 0){
            throw new CustomException("账号已禁用");
            //return R.error("账号已禁用");
        }

        //4. 更新用户的最后登录时间和最后登录ip
//        sUser.setLogin_ip(request.getRemoteAddr());
//        sUser.setLogin_date(new Date());
//        sysUserService.updateById(sUser);

        //5. 登录成功,返回token
        Map<String,Object> tokenMap = new HashMap<>();
        //将当前用户的id和对应的角色id写入token中
        tokenMap.put("userId",sUser.getId());
        tokenMap.put("roldId",sUser.getRole_id());
        String token = JwtUtils.getToken(tokenMap);

        Map<String,Object> resMap = new HashMap<>();
        resMap.put("token",token);
        return R.success(resMap);
    }

    @GetMapping("getConfig")
    public R getConfig(){
        //查询管理系统配置
        LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysConfig::getTab_value,"system");
        List<SysConfig> list = sysConfigService.list(wrapper);
        //组装数据返回
        Map<String,String> resMap = new HashMap<>();
        list.forEach(sysConfig -> resMap.put(sysConfig.getAttr_key(),sysConfig.getAttr_value()));
        return R.success(resMap);
    }

//    @GetMapping("getConfig")
//    public String getConfig(){
//        return "{\"errno\":0,\"errmsg\":\"\",\"data\":{\"login_title\":\"优点达资讯\",\"login_bg\":\"static/admin/image/loginbg.png\",\"system_name\":\"优点达资讯系统\",\"system_logo\":\"static/admin/image/logo.png\",\"web_ico\":\"static/admin/image/favicon.ico\"}}";
//    }


    @PostMapping("profile")
    public String getProfile(){
        return "{\"errno\":0,\"errmsg\":\"\",\"data\":{\"userInfo\":{\"id\":\"1\",\"login_name\":\"admin\",\"password\":\"96e79218965eb72c92a549dd5a330112\",\"name\":\"ThinkJS一哥\",\"email\":\"hewu1122@163.com\",\"phone\":\"17342065909\",\"login_ip\":\"::1\",\"login_date\":\"2018-01-08 16:22:51\",\"create_date\":\"2017-12-14 11:52:46\",\"update_date\":\"2022-11-13 11:42:38\",\"del_flag\":\"0\",\"role_id\":[\"1c54e003c1fc4dcd9b087ef8d48abac3\",\"营销主管\",\"反对反对\",\"社会牛肉人\"],\"status\":1,\"default_data\":\"1\",\"rolename\":[\"超级管理员\"]},\"menuList\":[{\"id\":2,\"pid\":1,\"path\":\"/system_menu\",\"component\":\"system/menu\",\"title\":\"菜单管理\",\"icon\":\"menu\",\"is_show\":1,\"is_cache\":0,\"is_link\":0,\"redirect\":null,\"create_date\":\"2017-12-15 15:50:39\",\"update_date\":\"2022-11-14 17:10:53\",\"del_flag\":0,\"type\":1,\"sort\":1,\"mark\":\"sys_menu\",\"default_data\":\"1\"},{\"id\":51,\"pid\":1,\"path\":\"/system_role\",\"component\":\"system/role\",\"title\":\"角色管理\",\"icon\":\"role\",\"is_show\":1,\"is_cache\":0,\"is_link\":0,\"redirect\":null,\"create_date\":\"2018-01-10 14:46:28\",\"update_date\":\"2021-07-26 17:01:57\",\"del_flag\":0,\"type\":1,\"sort\":2,\"mark\":\"sys_role\",\"default_data\":\"1\"},{\"id\":1,\"pid\":0,\"path\":\"/system\",\"component\":\"layout\",\"title\":\"系统设置\",\"icon\":\"setting\",\"is_show\":1,\"is_cache\":1,\"is_link\":1,\"redirect\":\"/system_menu\",\"create_date\":\"2017-12-15 15:48:13\",\"update_date\":\"2022-11-14 20:47:58\",\"del_flag\":0,\"type\":1,\"sort\":2,\"mark\":\"system\",\"default_data\":\"1\"},{\"id\":49,\"pid\":1,\"path\":\"/system_user\",\"component\":\"system/user\",\"title\":\"用户管理\",\"icon\":\"user\",\"is_show\":1,\"is_cache\":0,\"is_link\":0,\"redirect\":null,\"create_date\":\"2018-01-08 16:16:56\",\"update_date\":\"2021-07-26 17:01:47\",\"del_flag\":0,\"type\":1,\"sort\":3,\"mark\":\"sys_user\",\"default_data\":\"1\"},{\"id\":41,\"pid\":1,\"path\":\"/system_dict\",\"component\":\"system/dict\",\"title\":\"字典管理\",\"icon\":\"dict\",\"is_show\":0,\"is_cache\":0,\"is_link\":0,\"redirect\":null,\"create_date\":\"2017-12-25 23:19:01\",\"update_date\":\"2022-11-13 17:05:50\",\"del_flag\":0,\"type\":1,\"sort\":5,\"mark\":\"sys_dict\",\"default_data\":\"1\"},{\"id\":130,\"pid\":128,\"path\":\"/content_cate\",\"component\":\"content/cate\",\"title\":\"分类管理\",\"icon\":\"tree\",\"is_show\":1,\"is_cache\":1,\"is_link\":0,\"redirect\":\"\",\"create_date\":\"2021-09-25 00:49:25\",\"update_date\":\"2022-11-08 10:12:40\",\"del_flag\":0,\"type\":1,\"sort\":9,\"mark\":\"ydd_cate\",\"default_data\":\"1\"},{\"id\":115,\"pid\":1,\"path\":\"/system_config\",\"component\":\"system/config\",\"title\":\"系统配置\",\"icon\":\"set\",\"is_show\":1,\"is_cache\":1,\"is_link\":0,\"redirect\":\"\",\"create_date\":\"2021-07-26 19:31:55\",\"update_date\":\"2021-08-16 07:18:30\",\"del_flag\":0,\"type\":1,\"sort\":10,\"mark\":\"sys_config\",\"default_data\":\"1\"},{\"id\":128,\"pid\":0,\"path\":\"/content\",\"component\":\"layout\",\"title\":\"内容管理\",\"icon\":\"dict\",\"is_show\":1,\"is_cache\":1,\"is_link\":0,\"redirect\":\"\",\"create_date\":\"2021-09-25 00:26:52\",\"update_date\":\"2022-11-13 09:46:09\",\"del_flag\":0,\"type\":1,\"sort\":10,\"mark\":\"ydd_content\",\"default_data\":\"1\"},{\"id\":129,\"pid\":128,\"path\":\"/content_tag\",\"component\":\"content/tag\",\"title\":\"标签管理\",\"icon\":\"link\",\"is_show\":1,\"is_cache\":1,\"is_link\":0,\"redirect\":\"\",\"create_date\":\"2021-09-25 00:28:22\",\"update_date\":\"2021-09-26 23:25:57\",\"del_flag\":0,\"type\":1,\"sort\":10,\"mark\":\"ydd_tag\",\"default_data\":\"1\"},{\"id\":112,\"pid\":1,\"path\":\"/sys_generator\",\"component\":\"system/generator\",\"title\":\"代码生成器\",\"icon\":\"size\",\"is_show\":0,\"is_cache\":1,\"is_link\":0,\"redirect\":\"\",\"create_date\":\"2021-07-24 23:22:51\",\"update_date\":\"2021-10-27 21:52:05\",\"del_flag\":0,\"type\":1,\"sort\":10,\"mark\":\"sys_generator\",\"default_data\":\"1\"},{\"id\":131,\"pid\":128,\"path\":\"/content_article\",\"component\":\"content/article\",\"title\":\"文章管理\",\"icon\":\"language\",\"is_show\":1,\"is_cache\":1,\"is_link\":0,\"redirect\":\"\",\"create_date\":\"2021-09-25 22:49:32\",\"update_date\":\"2022-11-08 10:13:14\",\"del_flag\":0,\"type\":1,\"sort\":10,\"mark\":\"ydd_article\",\"default_data\":\"1\"},{\"id\":132,\"pid\":0,\"path\":\"/adv\",\"component\":\"layout\",\"title\":\"广告管理\",\"icon\":\"eye-open\",\"is_show\":1,\"is_cache\":1,\"is_link\":0,\"redirect\":\"\",\"create_date\":\"2021-09-27 22:23:54\",\"update_date\":\"2022-11-07 11:25:40\",\"del_flag\":0,\"type\":1,\"sort\":10,\"mark\":\"ydd_adv\",\"default_data\":\"1\"},{\"id\":133,\"pid\":132,\"path\":\"/adv_pos\",\"component\":\"adv/pos\",\"title\":\"广告位管理\",\"icon\":\"link\",\"is_show\":1,\"is_cache\":1,\"is_link\":0,\"redirect\":\"\",\"create_date\":\"2021-09-27 22:25:01\",\"update_date\":\"2021-10-21 23:15:29\",\"del_flag\":0,\"type\":1,\"sort\":10,\"mark\":\"ydd_advpos\",\"default_data\":\"1\"},{\"id\":134,\"pid\":132,\"path\":\"/adv_img\",\"component\":\"adv/img\",\"title\":\"广告图管理\",\"icon\":\"form\",\"is_show\":1,\"is_cache\":1,\"is_link\":0,\"redirect\":\"\",\"create_date\":\"2021-09-27 22:25:58\",\"update_date\":\"2021-10-21 23:15:36\",\"del_flag\":0,\"type\":1,\"sort\":10,\"mark\":\"ydd_advimg\",\"default_data\":\"1\"},{\"id\":135,\"pid\":128,\"path\":\"/content_article/add\",\"component\":\"content/article/info\",\"title\":\"添加文章\",\"icon\":\"\",\"is_show\":0,\"is_cache\":1,\"is_link\":0,\"redirect\":\"\",\"create_date\":\"2021-10-17 23:26:18\",\"update_date\":\"2021-11-03 09:58:36\",\"del_flag\":0,\"type\":1,\"sort\":10,\"mark\":\"content_article_add\",\"default_data\":\"1\"},{\"id\":136,\"pid\":128,\"path\":\"/content_article/edit/:id\",\"component\":\"content/article/info\",\"title\":\"修改文章\",\"icon\":\"\",\"is_show\":0,\"is_cache\":1,\"is_link\":0,\"redirect\":\"\",\"create_date\":\"2021-10-20 22:31:35\",\"update_date\":\"2021-11-03 09:58:42\",\"del_flag\":0,\"type\":1,\"sort\":10,\"mark\":\"content_article_edit\",\"default_data\":\"1\"},{\"id\":137,\"pid\":132,\"path\":\"/adv_link\",\"component\":\"adv/link\",\"title\":\"友情链接管理\",\"icon\":\"menu\",\"is_show\":1,\"is_cache\":1,\"is_link\":0,\"redirect\":\"\",\"create_date\":\"2021-10-21 23:14:44\",\"update_date\":\"2021-11-03 10:20:09\",\"del_flag\":0,\"type\":1,\"sort\":10,\"mark\":\"ydd_link\",\"default_data\":\"1\"},{\"id\":138,\"pid\":0,\"path\":\"/user\",\"component\":\"layout\",\"title\":\"会员管理\",\"icon\":\"user\",\"is_show\":1,\"is_cache\":1,\"is_link\":0,\"redirect\":\"\",\"create_date\":\"2021-10-27 20:57:58\",\"update_date\":\"2021-10-27 21:01:24\",\"del_flag\":0,\"type\":1,\"sort\":10,\"mark\":\"ydd_manage\",\"default_data\":\"1\"},{\"id\":139,\"pid\":138,\"path\":\"/user_user\",\"component\":\"user/user\",\"title\":\"用户管理\",\"icon\":\"role\",\"is_show\":1,\"is_cache\":1,\"is_link\":0,\"redirect\":\"\",\"create_date\":\"2021-10-27 21:00:41\",\"update_date\":\"2022-11-10 10:52:25\",\"del_flag\":0,\"type\":1,\"sort\":10,\"mark\":\"ydd_user\",\"default_data\":\"1\"}],\"dictsList\":{\"table_type\":{\"0\":\"单表\",\"1\":\"主表\",\"2\":\"附表\",\"3\":\"树结构表\"},\"interface_type\":{\"get\":\"get\",\"post\":\"post\"},\"report_type\":{\"1\":\"正常报告\",\"2\":\"异常报告\"},\"theme\":{\"ace\":\"ACE风格\"},\"order_type\":{\"1\":\"支付宝\",\"2\":\"微信\"}},\"btnList\":[\"*\"]}}";
    }

//    @GetMapping("baseInfo")
//    public String getBaseInfo(){
//        return "{\"errno\":0,\"errmsg\":\"\",\"data\":{\"all_cate\":[{\"id\":3,\"catename\":\"新闻\",\"num\":30},{\"id\":4,\"catename\":\"图片\",\"num\":37},{\"id\":5,\"catename\":\"娱乐\",\"num\":44},{\"id\":6,\"catename\":\"汽车\",\"num\":43},{\"id\":7,\"catename\":\"段子111\",\"num\":42},{\"id\":8,\"catename\":\"科技\",\"num\":36},{\"id\":9,\"catename\":\"时尚1\",\"num\":25}],\"advpos_num\":18,\"tag_num\":20,\"admin_num\":12,\"article_num\":258,\"cate_num\":7,\"advimg_num\":22,\"new_user\":[{\"id\":433,\"username\":\"13965032341\",\"sex\":1,\"icon\":null,\"address\":\"北京市 西城区\"},{\"id\":432,\"username\":\"王五\",\"sex\":0,\"icon\":\"\",\"address\":null},{\"id\":430,\"username\":\"\",\"sex\":0,\"icon\":\"upload/images/20221114/1668409012523.jpg\",\"address\":null},{\"id\":429,\"username\":\"18895682832\",\"sex\":1,\"icon\":null,\"address\":\"北京市 东城区\"},{\"id\":428,\"username\":\"18895682834\",\"sex\":1,\"icon\":null,\"address\":null},{\"id\":426,\"username\":\"发\",\"sex\":1,\"icon\":\"uploadimages202211131668336030097.png\",\"address\":null},{\"id\":424,\"username\":\"13965032343\",\"sex\":1,\"icon\":null,\"address\":null}],\"hot_article\":[{\"id\":404,\"title\":\"7979999999\",\"author\":\"7979999999\",\"keywords\":\"79799999997979999999797999999979799999997979999999797999999979799999997979999999\",\"click\":7979999999},{\"id\":397,\"title\":\"555555555\",\"author\":\"33333333333\",\"keywords\":null,\"click\":333333333},{\"id\":400,\"title\":\"44444444444\",\"author\":\"77777777\",\"keywords\":\"666666666666666666666666666\",\"click\":77777777},{\"id\":174,\"title\":\"李连杰回忆濒临死亡经历：大小便失禁，愿花100万买氧气包\",\"author\":\"落夜\",\"keywords\":\"李连杰\",\"click\":1199},{\"id\":44,\"title\":\"福特全新SUV正式发布，命名“领界”，外形精致，内饰很有档次感！\",\"author\":\"admin\",\"keywords\":\"福特，suv\",\"click\":1192},{\"id\":228,\"title\":\"菊花什么时候开\",\"author\":\"admin\",\"keywords\":\"菊花 ,，老师 ， 同学 ， 厕所\",\"click\":1191},{\"id\":171,\"title\":\"俄罗斯大学校花，凭借一双长腿，一夜之间爆红网络\",\"author\":\"admin\",\"keywords\":\"大学校花，长腿\",\"click\":1191}],\"user\":{\"total\":138,\"user_man\":75,\"user_woman\":61}}}";
//    }

    @GetMapping("/baseInfo")
    public R getBaseInfo(){

        Map map = new HashMap();

        //用户数量
        List<SysUser> users = sysUserService.list();
        int admin_num = users.size();
        map.put("admin_num",admin_num);

        //advimg数量
        List<YddAdvimg> advimgList = yddAdvimgService.list();
        int advimg_num = advimgList.size();
        map.put("advimg_num",advimg_num);
        //获取advpos数量
        List<YddAdvpos> advposList = yddAdvposService.list();
        int advpos_num = advposList.size();
        map.put("advpos_num",advpos_num);
        //获取all_cate
        QueryWrapper<YddCate> qw = new QueryWrapper<>();
        qw.select("id","catename");
        //查询所有的cate分类
        List<YddCate> cateList = yddCateService.list(qw);
        cateList.stream().map(item->{
            //获取分类的数量set到cate类中根据cateid
            LambdaQueryWrapper<YddArticle> lq = new LambdaQueryWrapper<>();
            lq.eq(YddArticle::getCateid,item.getId());
            List<YddArticle> articleList = yddArticleService.list(lq);
            item.setNum(articleList.size());
            return item;
        }).collect(Collectors.toList());
        map.put("all_cate",cateList);
        //获取article数量
        List<YddArticle> articleList = yddArticleService.list();
        int article_num = articleList.size();

        map.put("article_num",article_num);
        //获取cate_num
        int cate_num = cateList.size();
        map.put("cate_num",cate_num);
        //获取article的集合
        QueryWrapper<YddArticle> wrapper1 = new QueryWrapper<>();
        wrapper1.select("author","click","id","keywords","title");
        wrapper1.orderByDesc("click");
        wrapper1.last("limit 7");
        List<YddArticle> articles = yddArticleService.list(wrapper1);

        map.put("hot_article",articles);

        //获取新用户
        LambdaQueryWrapper<YddUser> lqw1 = new LambdaQueryWrapper<>();
        lqw1.isNull(YddUser::getBirthday);
        List<YddUser> userList = yddUserService.list(lqw1);
        map.put("new_user",userList);

        //获取tag_num
        List<YddTag> tagList = yddTagService.list();
        int tag_num = tagList.size();
        map.put("tag_num",tag_num);

        //获取用户分类数量
        List<YddUser> yddUserList = yddUserService.list();
        int total = yddUserList.size();

        //获取男用户数量
        LambdaQueryWrapper<YddUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YddUser::getSex,0);
        List<YddUser> yddUserListMan = yddUserService.list(lqw);
        int user_man = yddUserListMan.size();
        //获取女用户数量
        LambdaQueryWrapper<YddUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddUser::getSex,1);
        List<YddUser> yddUserListWoMan = yddUserService.list(wrapper);
        int user_woman = yddUserListWoMan.size();
        Map reMap = new HashMap();
        reMap.put("total",total);
        reMap.put("user_man",user_man);
        reMap.put("user_woman",user_woman);
        map.put("user",reMap);
        return R.success(map);
    }

}
